11 const double infinity
= 1E20
;
15 point(double X
, double Y
){ x
= X
; y
= Y
;}
18 map
< point
, double > dist
;
20 bool operator ==(const point
&a
, const point
&b
){ return (a
.x
== b
.x
&& a
.y
== b
.y
);}
21 bool operator !=(const point
&a
, const point
&b
){ return !(a
== b
);}
22 bool operator <(const point
&a
, const point
&b
){ return (a
.x
< b
.x
|| (a
.x
== b
.x
&& a
.y
< b
.y
));}
23 //bool operator <(point a, point b){ return (a.dist > b.dist);}
24 double distancia(point a
, point b
){return hypot(a
.y
-b
.y
, a
.x
-b
.x
);}
28 bool heapCmp(const point
&a
,const point
&b
){
29 return (dist
[a
] > dist
[b
]);
33 struct heapCompare
: public binary_function
<point
, point
, bool>
35 bool operator()(const point
&x
, const point
&y
) const
36 { return dist
[x
] > dist
[y
]; }
41 //contiene todos los nodos sueltos
43 //contiene un vector con todos los vecinos para el punto point
44 map
< point
, vector
<point
> > vecinos
;
47 if (find(nodos
.begin(), nodos
.end(), a
) != nodos
.end()) return; //Ya insertamos este nodo
49 for (int i
=0; i
<nodos
.size(); ++i
){
50 //Insertarlo como vecino en los nodos diferentes a el mismo
52 vector
<point
> adj
= vecinos
[nodos
[i
]];
53 //Asegurarse de no insertar un vecino repetido
54 if (find(adj
.begin(), adj
.end(), a
) == adj
.end()){
55 if (distancia(a
, nodos
[i
]) < 1.5){
56 //printf("Insertando (%f,%f) como vecino de (%f,%f).\n", a.x, a.y, nodos[i].x, nodos[i].y);
57 vecinos
[nodos
[i
]].push_back(a
);
58 vecinos
[a
].push_back(nodos
[i
]);
67 for (int i
=0; i
<nodos
.size(); ++i
){
68 dist
[nodos
[i
]] = infinity
;
69 if (nodos
[i
].x
== 0.00 && nodos
[i
].y
== 0.00){
70 dist
[nodos
[i
]] = 0.00;
75 /*void relax(point u, point v){
76 if (dist[v] > dist[u] + distancia(u, v)){
77 dist[v] = dist[u] + distancia(u, v);
81 void dijkstra(const double &maxPath
, const point
&finalPoint
){
84 //priority_queue<point, vector<point>, heapCompare > q(nodos.begin(), nodos.end());
85 priority_queue
<point
, vector
<point
>, heapCompare
> q
;
86 q
.push(point(0.0, 0.0));
87 //vector<point> q(nodos.begin(), nodos.end());
88 //make_heap(q.begin(), q.end(), heapCmp);
90 //point u = q.front();
91 //pop_heap(q.begin(), q.end(), heapCmp); q.pop_back();
94 //printf("Saque (%f,%f) cuya distancia es %f\n", u.x, u.y, dist[u]);
95 if (distancia(point(0.00, 0.00), u
) + distancia(u
, finalPoint
) <= maxPath
){
96 for (int i
=0; i
<vecinos
[u
].size(); ++i
){
97 point v
= vecinos
[u
][i
];
98 //printf(" Comparando (%f,%f) con (%f,%f) que estan a %f\n", u.x, u.y, v.x, v.y, distancia(u,v));
99 //printf(" dist[u] es %f, dist[v] es %f\n", dist[u], dist[v]);
100 if (dist
[vecinos
[u
][i
]] > (dist
[u
] + distancia(u
,v
))){
101 //printf(" Actualizando la distancia de v = (%f,%f)\n", v.x, v.y);
102 dist
[vecinos
[u
][i
]] = dist
[u
] + distancia(u
, v
);
103 //printf(" Nueva distancia de v: %f\n", dist[v]);
108 //make_heap(q.begin(), q.end(), heapCmp);
120 for (s
= ""; s
== ""; getline(cin
, s
));
131 g
.insert(point((double)w
, (double)h
));
132 g
.insert(point(0.00, 0.00));
135 for (int i
=0; i
<noPuntos
; ++i
){
138 g
.insert(point(x
,y
));
141 cout
<< "Termine de insertar todos los nodos.\n";
146 /*printf("Voy a imprimir el grafo de %d nodos:\n", g.nodos.size());
147 for (int i=0; i<g.nodos.size(); ++i){
148 printf("Estos son los vecinos de (%f,%f):\n", g.nodos[i].x, g.nodos[i].y);
149 for (int j=0; j<g.vecinos[g.nodos[i]].size(); ++j){
150 printf(" (%f,%f)\n", g.vecinos[g.nodos[i]][j].x, g.vecinos[g.nodos[i]][j].y);
154 g
.dijkstra(maximoCamino
, point((double)w
, (double)h
));
155 //printf("La distancia minima hasta (%d,%d) es %f.\n", w,h,dist[point((double)w, (double)h)]);
156 if (dist
[point((double)w
, (double)h
)] < maximoCamino
){
157 printf("I am lucky!\n");